use std::{fs::File, io::Read, path::PathBuf};

pub struct TestUtil;

impl TestUtil {
    pub fn build_module_plugin(root_path: &PathBuf) {
        let dylib_cargo_path = format!(
            "--manifest-path={}/module/mdm-manager/Cargo.toml",
            root_path.to_str().unwrap()
        );
        let mut cargo = std::process::Command::new("cargo");
        let _ = cargo.arg("build");
        let _ = cargo.arg(dylib_cargo_path);
        let output = cargo
            .stderr(std::process::Stdio::inherit())
            .output()
            .expect("Failed to invoke cargo");
        if !output.status.success() {
            std::process::exit(output.status.code().unwrap_or(101));
        }
    }

    pub fn read_restapi_example(file_dir: &PathBuf, file_name: &str) -> String {
        let mut file_path = PathBuf::from(file_dir);
        file_path.push(".iosp");
        file_path.push("restapi_example");
        file_path.push(file_name);
        file_path.push("request.json");

        let mut file = File::open(file_path.as_path())
            .expect(format!("can not find file: {}", file_path.to_string_lossy()).as_str());
        let mut json = String::new();
        file.read_to_string(&mut json).expect("read json faield");
        json
    }
}
